Checkboxes checked

Hello everyone,

I'm trying to make an interface where I have multiple "sections" (dbe?)  where I put checkboxes.

However I would like to get wich checkboxes are checked and change the value of some booleans with those results, the problem is that I have a global  window  with multiple "sections".

I tried the following code for one of my section :
 

 choice_action = label (my_main_window, " Choice of analyse : ")
 choice_action ->"left"->"form"
 
 string actions[] = {"Analyse 1",
     "Analyse 2 ",
     "Analyse 3"
                     } 
 
bool actionsChosen[] = {false, false, false} 

DBE actionCheck = checkBox(my_main_window, "",
                  actions, 3)  

 int bitmap = get actionCheck 
 print bitmap
    int remain  
    int i 
    for i in 0:2 do {
        remain = bitmap % 2      
        if (remain != 0) {
            actionsChosen[i] = true 
            if (i==0){
   Analyse1=true
   } else if (i==1){
   Analyse2 = true 
   } else {
   Analyse3 = true 
   }
   
        } else {
            actionsChosen[i] = false
        } 
        bitmap = bitmap / 2   
    }

If someone was able to help I would be very grateful !
Thank you


Liise - Tue Jul 19 08:14:07 EDT 2016

Re: Checbox
O.Wilkop - Tue Jul 19 08:29:48 EDT 2016

Hi,

 

you have two options: 

1. Let every checkbox only have 1 value, then you can easily check if the value is 0 or 1 for unchecked/checked. Using this method you will end up with n checkbox-DBEs.

 

2. Do it like you are doing right now, with several options in a single checkbox DBE. If you then want to retrieve values you have to use the bit-wise and operator "&" to check which checkbox is selected: 

int bitmap = get(checkBox);

int firstOption         = bitmap & 1
int secondOption        = bitmap & 2
int thirdOption         = bitmap & 4
int fourthOption        = bitmap & 8
int fifthOption         = bitmap & 16
int sixthOption         = bitmap & 32
int seventhOption       = bitmap & 64
int eighthOption        = bitmap & 128

You can then check if the value is 0 for unchecked or >0 for checked.

 

For each new option you'd have to double the value after the "&" (so 256 is next). For more information on how this works, google "binary operator and". It boils down to the number being represented as a binary value (so the value "77" becomes "1001101"), and using the &-operator you can check if a certain bit is set or not. In the example of "77" or "1001101", the first, third, fourth and seventh bit would be set (first bit is on the right), which means that these options are checked in your DBE.

Re: Checkboxes checked
Liise - Tue Jul 19 08:50:57 EDT 2016

Thank you for your help.

However I still have a problem, it seems like it takes into account only the randomly checked checkboxes   before  I check the boxes on my own.
Is my code badly written ?  I'm sorry I'm new in the DXL langage and this principle of checkbox is completely new for me !

choice_action = label (my_main_window, " Choice : ")
choice_action ->"left"->"form"
 
 string actions[] = {"Analyse1",
     "Analyse2",
     "Analyse3"
                     } 
 
bool actionsChosen[] = {false, false, false} 

DBE actionCheck = checkBox(my_main_window, "",
                  actions, 3)  
      
int bitmap = get (actionCheck) 
int firstOption         = bitmap & 1
print firstOption
int secondOption        = bitmap & 2
print secondOption
int thirdOption         = bitmap & 4
print thirdOption



 

Re: Checkboxes checked
O.Wilkop - Tue Jul 19 09:02:16 EDT 2016

Liise - Tue Jul 19 08:50:57 EDT 2016

Thank you for your help.

However I still have a problem, it seems like it takes into account only the randomly checked checkboxes   before  I check the boxes on my own.
Is my code badly written ?  I'm sorry I'm new in the DXL langage and this principle of checkbox is completely new for me !

choice_action = label (my_main_window, " Choice : ")
choice_action ->"left"->"form"
 
 string actions[] = {"Analyse1",
     "Analyse2",
     "Analyse3"
                     } 
 
bool actionsChosen[] = {false, false, false} 

DBE actionCheck = checkBox(my_main_window, "",
                  actions, 3)  
      
int bitmap = get (actionCheck) 
int firstOption         = bitmap & 1
print firstOption
int secondOption        = bitmap & 2
print secondOption
int thirdOption         = bitmap & 4
print thirdOption



 

well the way your code is right now you print the checkbox-result right after setting them to "3" (which in bit btw is "11", therefore option 1 and 2 will be checked, if you want option 3 to be selected only you have to either set it to "4" or "0b100").

If you want to check your checkbox result later, you need to do the check in some kind of callback, for example after the user presses a button. Another way is to attach a callback to the checkbox itself, this way you have a function in your code that will get called as soon as the user clicks any of the checkboxes.

 

Button example:

DBE dbeCheckbox = null;

void buttonCallback(DBE button){
        print "button pressed!\n";
        int bitmap = get(dbeCheckbox);
        int firstOption         = bitmap & 1
        int secondOption        = bitmap & 2
        int thirdOption         = bitmap & 4

        if(firstOption > 0){
                print "option 1 is selected\n";
        }
        if(secondOption > 0){
                print "option 2 is selected\n";
        }
        if(thirdOption > 0){
                print "option 3 is selected\n";
        }
}

DB main = centered("Test Dialog Box");
string actions[] = {"Analyse1", "Analyse2", "Analyse3"};
dbeCheckbox = checkBox(main, "", actions, 3);
button(main, "Click me", buttonCallback);

show(main);

 

Callback on the checkbox example:

DBE dbeCheckbox = null;

void checkboxCallback(DBE chkbox){
        print "selection changed!\n";
        int bitmap = get(dbeCheckbox);
        int firstOption         = bitmap & 1
        int secondOption        = bitmap & 2
        int thirdOption         = bitmap & 4

        if(firstOption > 0){
                print "option 1 is selected\n";
        }
        if(secondOption > 0){
                print "option 2 is selected\n";
        }
        if(thirdOption > 0){
                print "option 3 is selected\n";
        }
}

DB main = centered("Test Dialog Box");
string actions[] = {"Analyse1", "Analyse2", "Analyse3"};
dbeCheckbox = checkBox(main, "", actions, 3);
set(dbeCheckbox, checkboxCallback);

show(main);

 

Re: Checkboxes checked
Liise - Tue Jul 19 10:40:08 EDT 2016

Thank you very much for your help it works very well now !

Juste a little question : if I want to modify a global variable in this "callback function" is it possible ? I tried but this doesn't seem to work

Re: Checkboxes checked
O.Wilkop - Tue Jul 19 11:47:30 EDT 2016

Liise - Tue Jul 19 10:40:08 EDT 2016

Thank you very much for your help it works very well now !

Juste a little question : if I want to modify a global variable in this "callback function" is it possible ? I tried but this doesn't seem to work

It is possible, but you need to make sure that the global variable gets declared before the function that uses it:

void callback(){
    x=10;
}
int x = 0;

callback();

won't work, as the dxl-interpreter doesn't know that there is a global variable called "x" at the point where it looks at the function.

int x = 0;
void callback(){
    x=10;
}

callback();

however works, since the "x" is declared before the function that uses it. You might have to do the same thing with DBE elements if you ever plan to get() values off of them in a function.

To clarify: the value does not have to be initialized, it can be undefined, but the variable must at least be declared before the function that uses it.

Re: Checkboxes checked
Liise - Wed Jul 20 02:30:41 EDT 2016

But when I do my

set(dbeCheckbox, checkboxCallback);

even if I declared my booleans before, they don't change after the "call" of the function (but there values well change inside the function)

More precisely my code looks like this :

 

bool myBoolean 
string actions[] = {"analyse1", "analyse2", "analyse3"};
dbeCheckbox = checkBox(my_main_window, "", actions, 3);
set(dbeCheckbox, checkboxCallback);  

(in checkboxCallback I change 
the value of myBoolean)

print myBoolean

and there is an error ...

Re: Checkboxes checked
O.Wilkop - Wed Jul 20 05:14:16 EDT 2016

Liise - Wed Jul 20 02:30:41 EDT 2016

But when I do my

set(dbeCheckbox, checkboxCallback);

even if I declared my booleans before, they don't change after the "call" of the function (but there values well change inside the function)

More precisely my code looks like this :

 

bool myBoolean 
string actions[] = {"analyse1", "analyse2", "analyse3"};
dbeCheckbox = checkBox(my_main_window, "", actions, 3);
set(dbeCheckbox, checkboxCallback);  

(in checkboxCallback I change 
the value of myBoolean)

print myBoolean

and there is an error ...

I can't really tell where your problem is since your code is incomplete. Please post your complete code and/or the error message so I can tell you what's going wrong. If it's a lot of code or you aren't allowed to send out certain parts of it, you can remove those parts and make a smaller version that highlights your problem.

 

Your code above for example never calls show() or block() to display the dialog box. I also would've expected your "print myBoolean" part to be in some kind of button callback (when the user presses some kind of "start" button). Also remember that the function for the checkbox callback needs to be declared before the set() function that uses it.

 

Check out this example, it uses the set() callback on the checkboxes to fill the "actionsChosen" array with the values of the checkboxes and as soon as the user presses the "apply" button it will print the values accordingly.

 

DBE dbeCheckbox = null;

bool actionsChosen[] = {false, false, false};

void checkboxCallback(DBE chkbox){
        int bitmap = get(dbeCheckbox);
        int firstOption         = bitmap & 1
        int secondOption        = bitmap & 2
        int thirdOption         = bitmap & 4

        actionsChosen[0] = firstOption > 0;
        actionsChosen[1] = secondOption > 0;
        actionsChosen[2] = thirdOption > 0;
}

void buttonCallback(DB x){
        if(actionsChosen[0]){
                print "Start Analyse 1\n";
        }
        if(actionsChosen[1]){
                print "Start Analyse 2\n";
        }
        if(actionsChosen[2]){
                print "Start Analyse 3\n";
        }
}

DB main = centered("Test Dialog Box");
string actions[] = {"Analyse1", "Analyse2", "Analyse3"};
dbeCheckbox = checkBox(main, "", actions, 0);
set(dbeCheckbox, checkboxCallback);

apply(main, buttonCallback);

show(main);

If your program is supposed to work differently (without a button or similar), please tell me how you expect the program to work.

Re: Checkboxes checked
Liise - Thu Jul 21 02:45:11 EDT 2016

In fact I was trying to see my booleans in a bad way and not in the right place... my bad

Now it seems to work perfectly well and it's thanks to you !
I am so grateful for your help, thank you very much, I was a lost student and you didn't give me the impression to be annoyed by all my questions.

Have a good day and thank you again !